//package com.small.common.util.val.captcha;
//
//import com.small.common.util.cookie.CookieUtil;
//import org.apache.commons.lang3.StringUtils;
//import org.redisson.api.RMapCache;
//import org.redisson.api.RedissonClient;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.UUID;
//import java.util.concurrent.TimeUnit;
//
///**
// * 作者: fenny
// * 邮箱: 893146555@qq.com
// * 时间: 2017/11/23 19:09
// * 描述: 生成图形验证码
// */
//public class CaptchaGenerate {
//
//    private static final Logger LOGGER = LoggerFactory.getLogger(CaptchaGenerate.class);
//
//    private static final String MARKING = "marking";
//
//    private String cacheKey;
//
//    public CaptchaGenerate(String cacheKey) {
//        this.cacheKey = cacheKey;
//    }
//
//    @Resource
//    private RedissonClient redissonClient;
//
//    /**
//     * 生成验证码图片
//     *
//     * @param request  req
//     * @param response res
//     */
//    public void generate(HttpServletRequest request, HttpServletResponse response) {
//        RMapCache<String, CaptchaParam> rMapCache = redissonClient.getMapCache(cacheKey);
//        response.setHeader("Pragma", "no-cache");
//        response.setHeader("Cache-Control", "no-cache");
//        response.setDateHeader("Expires", 0);
//        response.setContentType("image/gif");
//        Captcha captcha = new CaptchaGif(150, 50, 5);
//        //从cookie中获取临时标示
//        String marking = CookieUtil.getCookie(request, MARKING);
//        //如果临时标示过期了,重新生成
//        if (StringUtils.isEmpty(marking) || !rMapCache.containsKey(marking)) {
//            marking = UUID.randomUUID().toString();
//        }
//        //将验证码对象,写入cookie
//        CookieUtil.addCookie(response, MARKING, marking, CookieUtil.MAX_AGE_MINUTE_5);
//        try {
//            //生成图形验证码
//            captcha.out(response.getOutputStream());
//            //生成验证码
//            CaptchaParam captchaParam = new CaptchaParam();
//            captchaParam.setValue(captcha.getText());
//            //~写入缓存
//            rMapCache.put(marking, captchaParam, 5, TimeUnit.MINUTES);
//        } catch (IOException e) {
//            LOGGER.error("生成验证码失败", e);
//        }
//    }
//
//    /**
//     * @param request req
//     * @param code    验证码
//     * @return true验证通过
//     */
//    public boolean check(HttpServletRequest request, String code) {
//        String marking = CookieUtil.getCookie(request, MARKING);
//        boolean result = marking != null;
//        if (result) {
//            RMapCache<String, CaptchaParam> rMapCache = redissonClient.getMapCache(cacheKey);
//            CaptchaParam captchaParam = rMapCache.get(marking);
//            result = captchaParam != null;
//            if (result) {
//                result = code.equalsIgnoreCase(captchaParam.getValue());
//                //不论验证结果,都要删除,重新获取
//                rMapCache.remove(marking);
//            }
//        }
//        return result;
//    }
//}
